<template>
  <div class="login" id="login" @keyup.enter="login">
    <el-form ref="loginRef" :model="loginForm" :rules="loginRules" class="login-form">
      <h3 class="title">XXX 后台管理系统</h3>
      <el-form-item  prop="username">
        <el-input type="text" v-model="loginForm.username" placeholder="用户名"></el-input>
      </el-form-item>
      <el-form-item  prop="password">
        <el-input  type="password" v-model="loginForm.password" placeholder="密码"></el-input>
      </el-form-item>
      <el-form-item class="login_btn">
        <el-button type="primary" size="large"  @click.prevent="login">登录</el-button>
      </el-form-item>
    </el-form>
  </div>

</template>

<script setup>
import {onMounted, ref} from "vue";
import {post} from '@/util/request'
import router from "@/router";

import qs from "qs";
import {useAppStore} from "@/store/modules/app";
onMounted(() => {
  const appStore = useAppStore();
  appStore.setHasRoutes(false);
  appStore.setToken("");
})
const loginRef = ref(null);
const loginForm = ref({
  username: "",
  password: "",
});
const loginRules = ref({
  username: [{required: true, message: "请输入用户名", trigger: "blur"}],
  password: [{required: true, message: "请输入密码", trigger: "blur"}],
});
const login = () => {
  loginRef.value.validate(async valid => {
    if (!valid) return;
    const data = await post("/login?"+qs.stringify(loginForm.value));
  //  console.log(data)
    if (data.code == 200) {
      const appStore = useAppStore();
      const token = data.token;
      const menu=data.menuList;
      const currentUser=data.currentUser;
/*      console.log("登录成功：token=" + token);

      console.log("登录成功：userInfo=" + JSON.stringify(currentUser));*/
      console.log("menuList=" + JSON.stringify(menu));
      appStore.setToken(token);
      appStore.setMenu(menu)
      appStore.setUserInfo(currentUser);
      await router.push("/")
    }
  })
}

</script>

<style lang="scss" scoped>
.login {
  width: 100%;
  display: flex;
  justify-content: center;
  align-items: center;
  height: 100%;
  //background-image: url("../assets/images/login-background.jpg");

  .login-form {
    .title{
      height: 50px;
      text-align: center;
      padding: 0px 50px;
    }
    border-radius: 6px;
    background: #ffffff;
    width: 400px;
    padding: 25px 25px 5px 25px;

    .el-input {
      height: 40px;
      input {
        display: inline-block;
        height: 40px;
      }
    }
  }

  .login_btn {
    padding: 14px 20px;
    width: 100%;
    font-size: 25px;
    display: flex;
    justify-content: center;
    align-items: center;
    height: 100%;
    .el-button {
      width: 100%;
    }
  }

}

</style>
